package com.delivery.adapter;

import java.util.ArrayList;
import java.util.List;

import com.delivery.activity.R;
import com.delivery.activity.RecommendActivity;
import com.delivery.activity.R.id;
import com.delivery.activity.R.layout;
import com.delivery.module.CategoriesContent;
import com.delivery.module.ConfigModule;
import com.delivery.utils.FileUtils;
import com.delivery.utils.GraphicUtils;
import com.delivery.utils.HttpDownloader;
import com.delivery.utils.StringUtils;

import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 
 * 要用用于生成显示数据
 * 
 * @author huangbq
 * 
 * 
 */

public class RecommendListViewAdapter extends BaseAdapter {
	
	private List<CategoriesContent> mList = new ArrayList<CategoriesContent>(); //一次显示的记录条数
	
	private Context mContext;
	
	private static final String TAG = "MainActivity";
	
	public int count = RecommendActivity.APP_PAGE_SIZE;
	
	public RecommendListViewAdapter(Context context){
		mContext = context;
	}
	
	public void addItem(final CategoriesContent item) {             
		mList.add(item);    
		notifyDataSetChanged();
	}
	
	public void clearList(){
		if(mList!=null){
			mList.clear();
		}
	}
	
	public int getCount() {
		return count;

	}

	public Object getItem(int pos) {

		return pos;

	}
	
	/*public listViewAdapter(Context context, List<CategoriesContent> list, int page) {
		mContext = context;
		pm = context.getPackageManager();
		
		if(mList!=null && mList.size() > 0){
			mList.clear();
		}
		
		mList = new ArrayList<CategoriesContent>();
		int i = page * APP_PAGE_SIZE;
		int iEnd = i+APP_PAGE_SIZE;
		while ((i<list.size()) && (i<iEnd)) {
			mList.add(list.get(i));
			i++;
		}
	}*/

	public long getItemId(int pos) {

		return pos;

	}

	public View getView(int position, View convertView, ViewGroup parent) {
		CategoriesContent categoriesContent = null;
		if(mList!=null && mList.size() > 0 && mList.size() > position){
			categoriesContent = mList.get(position);
		}
		AppItem appItem;
		View v = null;
		if (convertView == null) {
			 v = LayoutInflater.from(mContext).inflate(R.layout.twtj_info, null);
			
			appItem = new AppItem();
			appItem.mAppIcon = (ImageView)v.findViewById(R.id.imgUrl);
			appItem.imageTime = (TextView) v.findViewById(R.id.image_time);
			//appItem.contentDesc = (TextView) v.findViewById(R.id.contentDesc);
			appItem.contentDesc  = (TextView) v.findViewById(R.id.content_desc);
			//appItem.contentDesc.setText("33333333333333333333");
			v.setTag(appItem);
			//监听小类事件
			/*bt_zttj = (Button) v.findViewById(R.id.bt_zttj);
			bt_hjzp = (Button) v.findViewById(R.id.bt_hjzp);
			bt_mstj = (Button) v.findViewById(R.id.bt_mstj);*/
			
			/*bt_all.setOnClickListener(new TagQuery(0));
			bt_zttj.setOnClickListener(new TagQuery(R.categories.cate_zttj));
			bt_hjzp.setOnClickListener(new TagQuery(R.categories.cate_hjzp));
			bt_mstj.setOnClickListener(new TagQuery(R.categories.cate_mstj));*/
			convertView = v;
		} else {
			appItem = (AppItem)convertView.getTag();
		}
		
		if(categoriesContent == null){
			return convertView;
		}
		/*TextView text = (TextView) v.findViewById(R.id.content_desc);
		text.setText("33333333333333333333");*/
		HttpDownloader httpDownloader = new HttpDownloader();
		if(!FileUtils.isExistFile(categoriesContent.getLocalFileUrl())){
			int result = httpDownloader.downFile(ConfigModule.GET_Context_URL +categoriesContent.getImgUrl(),"/upload/",StringUtils.subStrIndexOf(categoriesContent.getImgUrl(),"/"));
		}
		
		Bitmap bit = GraphicUtils.CreateBitmapFromLocal(categoriesContent.getLocalFileUrl());
		appItem.mAppIcon.setImageBitmap(bit);
		//System.out.println("appItem.contentDesc"+appItem.contentDesc);
		appItem.contentDesc.setText(categoriesContent.getContentDesc());
		//System.out.println("appItem.appItem.imageTime"+appItem.contentDesc);
		//appItem.contentDesc.setText("作品日期："+StringUtils.subStrLen(categoriesContent.getUpdateTime(),10));
		return convertView;
	}
	
	/**
	 * 每个应用显示的内容，包括图标和名称
	 * @author Yao.GUET
	 *
	 */
	class AppItem {
		ImageView mAppIcon;
		TextView mAppName;
		TextView imageTime;
		TextView contentDesc;
	}

}
